<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目" prop="projectId">
        <el-select  v-model="queryParams.projectId" placeholder="======请选择项目======"  @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in projectList"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="工单标题" prop="woTitle">
        <el-input
          v-model="queryParams.woTitle"
          placeholder="请输入工单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="responsibleName">
        <el-input
          v-model="queryParams.responsibleName"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workorder:workorder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workorder:workorder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workorder:workorder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workorder:workorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="woId"  />
      <el-table-column label="类别" align="center" prop="woCatalogName" />
      <el-table-column label="项目" align="center" prop="projectName" />
      <el-table-column label="活动" align="center" prop="activityName" />
      <el-table-column label="编号" align="center" prop="woNumber" />
      <el-table-column label="开始时间" align="center" prop="woStart">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.woStart,'{y}年{m}月{d}日')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="woEnd" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.woEnd,'{y}年{m}月{d}日')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="woTag" />
      <el-table-column label="标题" align="center" prop="woTitle" />
      <el-table-column label="事由内容" align="center" prop="woContent">
        <template slot-scope="scope">
          <span>{{ scope.row.woContent.length > 5 ? scope.row.woContent.substr(0,5)+'...':scope.row.woContent}}</span>
          <el-button type="text" @click="showDetails(scope.row.woContent)">详情</el-button>
        </template>
      </el-table-column>
        <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="责任人" align="center" prop="responsibleName" />
      <el-table-column label="状态" align="center" prop="woStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.woStatus === 1" size="small">待处理</el-tag>
          <el-tag v-else-if="scope.row.woStatus === 2" size="small" type="success">通过</el-tag>
          <el-tag v-else-if="scope.row.woStatus === 3" size="small" type="danger">退回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="woDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.woDate, '{y}年{m}月{d}日') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workorder:workorder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workorder:workorder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="项目" prop="projectId" style="width: 430px;">-->
<!--              <el-cascader-->
<!--                style="width: 100%;"-->
<!--                v-model="cascaderValue"-->
<!--                :options="options"-->
<!--                @change="handleChange">-->
<!--              </el-cascader>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目主键" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择项目主键" @change="findAc">
                <el-option v-for="project in projectList" :key="project.projectId" :label="project.projectName"
                           :value="project.projectId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动主键" prop="avtivityId">
              <el-select v-model="form.avtivityId" placeholder="请选择活动主键">
                <el-option v-for="a in activityList" :key="a.avtivityId" :label="a.activityName"
                           :value="a.avtivityId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="woStart"  style="width: 430px;">
              <el-date-picker clearable
                              v-model="form.woStart"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 100%;"
                              placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止时间" prop="woEnd" style="width: 430px;">
              <el-date-picker clearable
                              v-model="form.woEnd"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 100%;"
                              placeholder="请选择截止时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别" prop="woCatalogId" style="width: 430px;">
              <el-select v-model="form.woCatalogId" placeholder="请选择工单类别"  style="width: 100%;"  >
                <el-option v-for="t in typeList"
                           :key="t.woCatalogId"
                           :label="t.woCatalogName"
                           :value="t.woCatalogId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标签" prop="woTag">
              <el-input v-model="form.woTag" placeholder="请输入工单标签" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="标题" prop="woTitle" width="220px">
          <el-input v-model="form.woTitle" placeholder="请输入工单标题" />
        </el-form-item>
        <el-form-item label="事由内容" prop="woContent">
          <el-input type="textarea" v-model="form.woContent" placeholder="请输入事由内容"  />
        </el-form-item>
        <el-row>
          <el-col :span="9">
            <el-form-item label="责任人" prop="responsibleId">
              <el-input v-model="form.responsibleName" placeholder="请输入责任人" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button style="margin-left: 10px;color: #0072c6" @click="showTree('1')">选择责任人</el-button>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="电话:" prop="responsiblePhone"  v-show="form.responsibleId != null">
              <el-input v-html="responsiblePhone" style="color:red"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="9">
            <el-form-item label="协助人" prop="helpId">
              <el-input v-model="form.helpName" placeholder="请输入协助人" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button style="margin-left: 10px;color: #1ab394" @click="showTree('2')">选择协助人</el-button>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="电话:" prop="helpPhone"  v-show="form.helpId != null">
              <el-input v-html="helpPhone" style="color:red"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="9">
            <el-form-item label="抄报人" prop="reporterId">
              <el-input v-model="form.reporterName" placeholder="请输入抄报人" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button style="margin-left: 10px" @click="showTree('3')">选择抄报人</el-button>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="电话:" prop="reporterPhone"  v-show="form.reporterId != null">
              <el-input v-html="reporterPhone" style="color:red"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="9">
            <el-form-item label="抄送人" prop="copyId">
              <el-input v-model="form.copyName" placeholder="请输入抄送人" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button style="margin-left: 10px" @click="showTree('4')">选择抄送人</el-button>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="电话:" prop="copyPhone"  v-show="form.copyId != null">
              <el-input v-html="copyPhone" style="color:red"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="图片">
          <image-upload v-model="form.images" :limit="3"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--详情弹框-->
    <el-dialog :visible.sync="dialogVisible" title="事由内容详情">
      <p>{{currentContent}}</p>
    </el-dialog>
    <!--树弹框-->
    <el-dialog ref="treeDialog" :visible.sync="openTree" :title="titleDialog(num)" style="margin-top:260px" width="500px" append-to-body>
      <project-dept-user-tree-selector
        :project-id="form.projectId"
        :showCheckbox=false
        @handleNodeClick="handleNodeClick"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listWorkorder, getWorkorder, delWorkorder, addWorkorder, updateWorkorder,getAct } from "@/api/workorder/workorder";
import {listProjectByUserId, listCascaderProjectForActList} from "@/api/project/project";
import {listWorkorderType} from "@/api/workorder/workorderType";
import ProjectDeptUserTreeSelector from "@/components/ProjectDeptUserTreeSelector/index.vue";
import {getPhone} from '@/api/system/user';
export default {
  name: "Workorder",
  components: {ProjectDeptUserTreeSelector},
  data() {
    return {
      helpName:null,
      helpPhone:null,
      responsibleName:null,
      responsiblePhone:null,
      reporterName:null,
      reporterPhone:null,
      copyName:null,
      copyPhone:null,
      // 表单中四个树的开关
      num:null,
      openTree: false,
      // 级联
      cascaderValue: [],
      options: [],
      // 详情模态窗
      dialogVisible: false,
      currentContent:null,
      /** 项目列表 */
      projectList: [],
      /** 项目活动列表 */
      activityList: [],
      /** 工单类别列表 */
      typeList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单表格数据
      workorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        woCatalogId: null,
        projectId: null,
        avtivityId: null,
        woNumber: null,
        woStart: null,
        woEnd: null,
        woTag: null,
        woTitle: null,
        woContent: null,
        createId: null,
        responsibleId: null,
        helpId: null,
        reporterId: null,
        copyId: null,
        woStatus: null,
        woDate: null,
        responsibleName:null,
        createName:null,
      },
      // 表单参数
      form: {
        projectId:null,
        avtivityId:null,
      },
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        woStart: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        woEnd: [
          { required: true, message: "截止时间不能为空", trigger: "blur" }
        ],
        woCatalogId: [
          { required: true, message: "工单类别不能为空", trigger: "blur" }
        ],
        woTag: [
          { min: 0, max: 50, message: "长度不大于50个字符", trigger: "blur" }
        ],
        woTitle: [
          { required: true, message: "工单标题不能为空", trigger: "blur" },
          { min: 0, max: 200, message: "长度不大于200个字符", trigger: "blur" }
        ],
        woContent: [
          { required: true, message: "事由内容不能为空", trigger: "blur" },
          { min: 0, max: 800, message: "长度不大于800个字符", trigger: "blur" }
        ],
        createId: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        responsibleId: [
          { required: true, message: "责任人不能为空", trigger: "blur" }]
      }
    };
  },
  mounted() {
    // 初始化树
    this.$refs.treeDialog.rendered = true;
  },
  created() {
    this.getList();
  },
  methods: {
    findAc(){
      getAct(this.form.projectId).then(res=>{
        this.activityList = res.data;
      }).catch(() => {});
    },
    //树的模态窗的标题
    titleDialog(num){
      switch (num){
        case "1":
          return "选择责任人";
        case "2":
          return "选择协助人";
        case "3":
          return "选择抄报人";
        case "4":
          return "选择抄送人";
      }
    },
   //打开树的模态窗
    showTree(num){
      this.num=num;
      this.openTree = true;
    },
    // 点击树节点 组件传值
    handleNodeClick(data){
      if(data.level === 4){
        switch (this.num){
          case "1":
            this.form.responsibleId = data.id;
            this.form.responsibleName = data.label;
            this.responsiblePhone = data.phonenumber;
            break;
          case "2":
            this.form.helpId = data.id;
            this.form.helpName = data.label;
            this.helpPhone = data.phonenumber;
            break;
          case "3":
            this.form.reporterId = data.id;
            this.form.reporterName = data.label;
            this.reporterPhone = data.phonenumber;
            break;
          case "4":
            this.form.copyId = data.id;
            this.form.copyName = data.label;
            this.copyPhone = data.phonenumber;
            break;
        }this.openTree = false;
      }else{
        this.$message.error("仅能选择员工");
      }
    },
    /** 展示详情 */
    showDetails(con) {
      this.currentContent = con;
      this.dialogVisible = true;
    },
    // 获取级联数据
    getCascader(){
      listCascaderProjectForActList().then(resp=>{
        this.options = resp.data;
      })
    },
    /** 查询项目下拉框 */
    getProjectByUserId(){
      listProjectByUserId().then(resp=>{
        this.projectList=resp.data
      })
    },
    /** 查询工单类型下拉框 */
    getWorkorderTypeList(){
      listWorkorderType().then(resp=>{
        this.typeList=resp.rows
      })
    },
    /** 查询工单列表 */
    getList() {
      this.loading = true;
      listWorkorder(this.queryParams).then(response => {
        this.workorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getWorkorderTypeList();
      this.getProjectByUserId();
      this.getCascader();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        woId: null,
        woCatalogId: null,
        projectId: null,
        avtivityId: null,
        woNumber: null,
        woStart: null,
        woEnd: null,
        woTag: null,
        woTitle: null,
        woContent: null,
        createId: null,
        responsibleId: null,
        helpId: null,
        reporterId: null,
        copyId: null,
        woStatus: null,
        woDate: null,
        responsibleName: null,
        helpName: null,
        reporterName: null,
        copyName: null,
        images:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.woId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //级联清空
      this.cascaderValue=[];
      this.open = true;
      this.title = "添加工单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const woId = row.woId || this.ids
      getWorkorder(woId).then(response => {
        this.form = response.data;
        //级联回显
        this.cascaderValue = [response.data.projectId, response.data.avtivityId]
        //回显手机号
        if(response.data.responsibleId!=null){
          getPhone(response.data.responsibleId).then(resp=>{
            this.responsiblePhone = resp.data[0].phonenumber;
          })
        }
        if(response.data.helpId!=null){
          getPhone(response.data.helpId).then(resp=>{
            this.helpPhone = resp.data[0].phonenumber;
          })
        }
        if(response.data.reporterId!=null){
          getPhone(response.data.reporterId).then(resp=>{
            this.reporterPhone = resp.data[0].phonenumber;
          })
        }
        if(response.data.copyId!=null){
          getPhone(response.data.copyId).then(resp=>{
            this.copyPhone = resp.data[0].phonenumber;
          })
        }
        this.open = true;
        this.title = "修改工单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.woId != null) {
            updateWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const woIds = row.woId || this.ids;
      this.$modal.confirm('是否确认删除工单编号为"' + woIds + '"的数据项？').then(function() {
        return delWorkorder(woIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('workorder/workorder/export', {
        ...this.queryParams
      }, `workorder_${new Date().getTime()}.xlsx`)
    },
    //级联选择器
    handleChange(value){
      this.form.projectId = value[0];
      this.form.avtivityId =value[1];
      this.resetPeopleData();
    },
    //切换项目后重置人员数据
    resetPeopleData(){
      this.form.responsibleId = null;
      this.form.responsibleName = null;
      this.responsiblePhone = null;

      this.form.helpId = null;
      this.form.helpName = null;
      this.helpPhone = null;

      this.form.reporterId = null;
      this.form.reporterName = null;
      this.reporterPhone = null;

      this.form.copyId = null;
      this.form.copyName = null;
      this.copyPhone = null;
    }
  }
};
</script>
